package com.mmall.concurrency.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class ThreadPoolExample5 {

    public static void main(String[] args) {

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(3, 4, 60L
                , TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(5));

        // 多个线程交叉执行，输出乱序
        // 当执行的任务数量大于最大线程池数量后，会将超过的线程放到阻塞队列中
        // 当放入到阻塞队列里面的线程大于容量的时候抛异常，这里指定了LinkedBlockingQueue的capacity为5
        // 显然应付10000个会抛异常！不填写capacity的话，就是默认无界
        for (int i = 0; i < 10000; i++) {
            final int num = i;
            poolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(num);
                }
            });
        }

        poolExecutor.shutdown();

    }
}
